﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 花店管理系统.geren
{
    public partial class Qianshou : Form
    {
        public Qianshou()
        {
            InitializeComponent();
        }
        SqlDataAdapter daQianshou;
        DataSet ds = new DataSet();
        private DataRow selectedRow;
        void init()
        {
            DB.GetCn();
            string str = "select * from Torder";
            daQianshou = new SqlDataAdapter(str, DB.cn);
            daQianshou.Fill(ds, "qianshou_info");
            DB.cn.Close();
        }

        void showAll()
        {
            init();
            showXz();
        }
        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvQianshou.Columns.Add(acCode);
        }

        private void Qianshou_Load(object sender, EventArgs e)
        {
            showAll();
            DataView dvOrder = new DataView(ds.Tables["qianshou_info"]);
            dgvQianshou.DataSource = dvOrder;
        }

        private void dgvQianshou_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvQianshou.Columns["acCode"].Index && e.RowIndex >= 0)
            {
                selectedRow = GetSelectedRow(e.RowIndex);
            }

            if (dgvQianshou.Rows.Count > 0)
            {
                for (int i = 0; i < dgvQianshou.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvQianshou.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
            }
        }

        private DataRow GetSelectedRow(int rowIndex)
        {
            var dataView = (DataView)dgvQianshou.DataSource;
            return dataView.Table.Rows[rowIndex];
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            int s = dgvQianshou.Rows.Count;
            for (int i = 0; i < dgvQianshou.Rows.Count; i++)
            {
                if (dgvQianshou.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    dgvQianshou.Rows[i].Cells["O_qianshou"].Value = "已签收";

                    UpdateDB();
                }
                else
                {
                    s = s - 1;
                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择要签收的项");
            }
        }

        void UpdateDB()//更新数据源
        {
            try
            {
                SqlCommandBuilder dbOrder = new SqlCommandBuilder(daQianshou);
                daQianshou.Update(ds, "Qianshou_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPinglun_Click(object sender, EventArgs e)
        {
            if (selectedRow != null)
            {
                geren.Pinglun pl = new geren.Pinglun();
                pl.CommentForOrder = selectedRow;
                pl.ShowDialog();
            }
            else
            {
                MessageBox.Show("请先选择要评论的订单");
            }
        }
    }
}
